package com.dp.appi.util;

import it.sauronsoftware.jave.*;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2016/6/28.
 */
public class Amr_mp3_format {
    public static void changeToMp3(String webURL,String sourcePath, String targetPath) throws IOException {
        String sb1 = (webURL+sourcePath).replace("\\","/");
        String sb2 = (webURL+targetPath).replace("\\","/");

        File source = new File(sb1);
        boolean flag = source.exists();
        if (flag) {
            File target = new File(sb2);
            AudioAttributes audio = new AudioAttributes();
            Encoder encoder = new Encoder();

            audio.setCodec("libmp3lame");

            audio.setBitRate(new Integer(128000));
            audio.setChannels(new Integer(2));
            audio.setSamplingRate(new Integer(44100));

            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp3");
            attrs.setAudioAttributes(audio);

            try {
                encoder.encode(source, target, attrs);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InputFormatException e) {
                e.printStackTrace();
            } catch (EncoderException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String source ="upload\\projectRadio\\20161121\\1479724835347.amr";
        String target ="upload\\projectRadio\\20161121\\1479724835347.mp3";
        try {
            changeToMp3("G:\\workspace\\IdeaProject\\pw\\designpass\\web\\",source,target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
